<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>有度客户端登录</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }

        .container {
            text-align: center;
            padding: 2rem;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            max-width: 400px;
            width: 90%;
        }

        .loading {
            display: block;
        }

        .error {
            display: none;
            color: #dc3545;
        }

        .download-btn {
            display: block;
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            margin-top: 1rem;
        }

        .download-btn:hover {
            background-color: #0056b3;
        }

        #downloadSection {
            margin-top: 3rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <div id="loading" class="loading">
            <h1 style="color: #007bff;"><span id="countdown">0</span> 秒后关闭</h1>
            <!-- <h1><span id="countdown">0</span> 秒后关闭</h1> -->
            <h2>正在启动并登录有度客户端...</h2>
            <!-- </div> -->
            <!-- <div id="error" class="error"> -->
            <!-- <h2>当前电脑尚未安装有度客户端</h2> -->
            <div id="downloadSection"></div>
        </div>
    </div>

    <script>
        const countdownElement = document.getElementById('countdown');
        let countdown = "{{ .Countdown }}"; // 从后端传入的倒计时秒数
        countdown = parseInt(countdown, 10); // 确保是整数
        if (isNaN(countdown) || countdown < 0) {
            countdown = 10; // 默认倒计时为10秒
        }

        const countdownInterval = setInterval(() => {
            countdownElement.textContent = countdown;
            countdown--;
            if (countdown < 0) {
                clearInterval(countdownInterval);
                // 如果倒计时结束，关闭当前页面
                window.close();
            }
        }, 1000);
    </script>

    <script>
        const tokenLink = "{{ .TokenLink }}";
        const clientUrl = "{{ .ClientUrl }}";


        // 尝试打开客户端
        window.location.href = tokenLink;

        // 5秒后检查是否成功打开客户端
        setTimeout(() => {


            // document.getElementById('loading').style.display = 'none';
            // document.getElementById('error').style.display = 'block';

            const downloadSection = document.getElementById('downloadSection');
            if (clientUrl) {
                downloadSection.innerHTML = `
                    <a href="${clientUrl}" class="download-btn" target="_blank">
                        如果未安装，请下载有度客户端
                    </a>
                `;
            } else {
                downloadSection.innerHTML = '<p>如果未安装，请联系管理员获取</p>';
            }
        }, 1);

        // // 监听窗口失去焦点
        // var blurCount = 0;
        // window.addEventListener('blur', () => {
        //     blurCount++;
        //     console.log('window.blur', blurCount);
        // });

        // var tid = setInterval(() => {
        //     // 如果失去焦点超过2次，就可能成功启动客户端，此时关闭当前页面
        //     if (blurCount > 1) {
        //         console.log("可能已安装");
        //         window.close();
        //         return;
        //     } else {
        //         console.log("未安装");
        //     }
        // }, 500);



    </script>
</body>

</html>